package com.ruoyi.common.utils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ExcelUtils {

    /**
     * @param is         is流
     * @param fileName   文件名
     * @param sheetIndex 页码
     * @return 页面的全部信息
     */
    public static Sheet getSheet(InputStream is, String fileName, int sheetIndex) {
        try {
            Workbook workbook = null;
            String hou = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            if ("xlsx".equals(hou)) {
                workbook = new XSSFWorkbook(is);
            } else if ("xls".equals(hou)) {
                workbook = new HSSFWorkbook(is);
            }
            if (workbook == null) {
                return null;
            }
            return workbook.getSheetAt(sheetIndex);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
